{include="header"}

{if condition="$fsc->suser"}
<script type="text/javascript">
   function fs_marcar_todo()
   {
      $("#f_user_pages input[name='enabled[]']").prop('checked', true);
   }
   function fs_marcar_nada()
   {
      $("#f_user_pages input[name='enabled[]']").prop('checked', false);
   }
   $(document).ready(function() {
      $("#b_eliminar_usuario").click(function(event) {
         event.preventDefault();
         if( confirm("¿Realmente desea eliminar el usuario?") )
            window.location.href = '{$fsc->ppage->url()}&delete={$fsc->suser->nick}';
      });
      $("#b_nuevo_agente").click(function(event) {
         event.preventDefault();
         $("#modal_nuevo_agente").modal('show');
      });
   });
</script>

<form class="form" role="form" id="f_user_pages" action="{$fsc->url()}" method="post">
   <input type="hidden" name="modupages" value="TRUE"/>
   <div class="container-fluid">
      <div class="row" style="margin-top: 10px; margin-bottom: 10px;">
         <div class="col-md-8">
            <a class="btn btn-sm btn-default" href="{$fsc->url()}" title="recargar la página">
               <span class="glyphicon glyphicon-refresh"></span>
            </a>
            <a class="btn btn-sm btn-default" href="{$fsc->ppage->url()}">
               <span class="glyphicon glyphicon-list"></span> &nbsp; Usuarios
            </a>
         </div>
         <div class="col-md-4 text-right">
            <div class="btn-group">
               {if condition="$fsc->user->nick != $fsc->suser->nick"}
               <a href="#" id="b_eliminar_usuario" class="btn btn-sm btn-danger">
                  <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
               </a>
               {/if}
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
      <div class="row">
         <div class="form-group col-lg-2 col-md-2 col-sm-2">
            Nick:
            <input class="form-control" type="text" name="snick" value="{$fsc->suser->nick}" disabled="disabled"/>
            <div class="checkbox">
               <label>
                  <input type="checkbox" name="sadmin" value="TRUE"{if condition="$fsc->suser->admin"} checked="checked"{/if}/>
                  Administrador
               </label>
            </div>
         </div>
         <div class="form-group col-lg-2 col-md-2 col-sm-2">
            Contraseña:
            <input class="form-control" type="password" name="spassword"/>
         </div>
         <div class="form-group col-lg-3 col-md-3 col-sm-3">
            <a href="{$fsc->agente->url()}">Empleado</a>
            <select name="scodagente" class="form-control">
               <option value="">Ninguno</option>
               {loop="$fsc->agente->all()"}
               <option value="{$value->codagente}"{if condition="$fsc->suser->codagente == $value->codagente"} selected="selected"{/if}>{$value->get_fullname()}</option>
               {/loop}
            </select>
            <p class="help-block">
               <a id="b_nuevo_agente" href="#">Crear un nuevo empleado y asignarlo a este usuario</a>
            </p>
         </div>
         <div class="form-group col-lg-2 col-md-2 col-sm-2">
            Página de inicio
            <select name="udpage" class="form-control">
            {loop="$fsc->suser->get_menu()"}
               {if condition="$value->show_on_menu"}
                  {if condition="$value->name==$fsc->suser->fs_page"}
                  <option value="{$value->name}" selected='selected'>{$value->folder} - {$value->title}</option>
                  {else}
                  <option value="{$value->name}">{$value->folder} - {$value->title}</option>
                  {/if}
               {/if}
            {/loop}
            </select>
         </div>
         <div class="form-group col-lg-1 col-md-1 col-sm-1">
            Ejercicio
            <select name="ejercicio" class="form-control">
            {loop="$fsc->ejercicio->all_abiertos()"}
               {if condition="$value->codejercicio==$fsc->suser->codejercicio"}
               <option value="{$value->codejercicio}" selected='selected'>{$value->nombre}</option>
               {else}
               <option value="{$value->codejercicio}">{$value->nombre}</option>
               {/if}
            {/loop}
            </select>
         </div>
         <div class="form-group col-lg-2 col-md-2 col-sm-2">
            Plantilla Web
            <select name="template" class="form-control">
            {loop="$fsc->suser->get_template()"}
               {if condition="$value['tempvalor']==$fsc->suser->template"}
               <option value="{$value['tempvalor']}" selected='selected'>{$value['template']}</option>
               {else}
               <option value="{$value['tempvalor']}">{$value['template']}</option>
               {/if}
            {/loop}
            </select>
         </div>
      </div>
   </div>
   
   <div class="table-responsive">
      <table class="table table-hover">
         <thead>
            <tr>
               <th class="text-left">Último login</th>
               <th class="text-left">IP</th>
               <th class="text-left">Navegador</th>
            </tr>
         </thead>
         <tr>
            <td>{$fsc->suser->show_last_login()}</td>
            <td>{$fsc->suser->last_ip}</td>
            <td>{$fsc->suser->last_browser}</td>
         </tr>
      </table>
   </div>
   
   <div role="tabpanel">
      <ul class="nav nav-tabs" role="tablist">
         <li role="presentation" class="active">
            <a href="#home" aria-controls="home" role="tab" data-toggle="tab">Autorizar</a>
         </li>
         <li role="presentation">
            <a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Historial</a>
         </li>
      </ul>
      <div class="tab-content">
         <div role="tabpanel" class="tab-pane active" id="home">
            <div class="table-responsive">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th width="100">Autorizado</th>
                        <th class="text-left">Página</th>
                        <th class="text-left">Menú</th>
                     </tr>
                  </thead>
                  {if condition="!$fsc->suser->admin"}
                     {loop="$fsc->all_pages()"}
                     <tr>
                        <td class="text-center">
                           <input type="checkbox" name="enabled[]" value="{$value->name}"{if condition="$value->enabled"} checked="checked"{/if}/>
                        </td>
                        <td>{$value->name}</td>
                        <td>{$value->folder} » {$value->title}</td>
                     </tr>
                     {/loop}
                  {else}
                  <tr class="bg-warning">
                     <td colspan="3">
                        Los administradores tienen acceso a cualquier página.
                     </td>
                  </tr>
                  {/if}
               </table>
            </div>
            {if condition="!$fsc->suser->admin"}
            <div class="container-fluid">
               <div class="row">
                  <div class="col-lg-6 col-md-6 col-sm-6">
                     <div class="btn-group">
                        <button class="btn btn-sm btn-default" type="button" onclick="fs_marcar_todo()" title="Marcar todo">
                           <span class="glyphicon glyphicon-check"></span>
                        </button>
                        <button class="btn btn-sm btn-default" type="button" onclick="fs_marcar_nada()" title="Desmarcar todo">
                           <span class="glyphicon glyphicon-unchecked"></span>
                        </button>
                     </div>
                  </div>
                  <div class="col-lg-6 col-md-6 col-sm-6 text-right">
                     <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                        <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
                     </button>
                  </div>
               </div>
            </div>
            {/if}
         </div>
         <div role="tabpanel" class="tab-pane" id="profile">
            <div class="table-responsive">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th class="text-left">Tipo</th>
                        <th class="text-left">Detalle</th>
                        <th class="text-left">IP</th>
                        <th class="text-right">Fecha</th>
                     </tr>
                  </thead>
                  {loop="$fsc->user_log"}
                  <tr{if condition="$value->alerta"} class="bg-danger"{/if}>
                     <td>{$value->tipo}</td>
                     <td>{$value->detalle}</td>
                     <td>{$value->ip}</td>
                     <td class="text-right">{$value->fecha}</td>
                  </tr>
                  {/loop}
               </table>
            </div>
         </div>
      </div>
   </div>
</form>

<form class="form-horizontal" role="form" name="f_nuevo_agente" action="{$fsc->url()}" method="post">
   <div class="modal fade" id="modal_nuevo_agente">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Nuevo empleado</h4>
            </div>
            <div class="modal-body">
               <div class="form-group">
                  <label for="ncodagente" class="col-lg-2 col-md-2 col-sm-2 control-label">Código</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="ncodagente" value="{$fsc->agente->get_new_codigo()}" autocomplete="off"/>
                  </div>
               </div>
               <div class="form-group">
                  <label for="nnombre" class="col-lg-2 col-md-2 col-sm-2 control-label">Nombre</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="nnombre" autocomplete="off" autofocus />
                  </div>
               </div>
               <div class="form-group">
                  <label for="napellidos" class="col-lg-2 col-md-2 col-sm-2 control-label">Apellidos</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="napellidos" autocomplete="off"/>
                  </div>
               </div>
               <div class="form-group">
                  <label for="ndnicif" class="col-lg-2 col-md-2 col-sm-2 control-label">{#FS_CIFNIF#}</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="ndnicif" autocomplete="off"/>
                  </div>
               </div>
               <div class="form-group">
                  <label for="ntelefono" class="col-lg-2 col-md-2 col-sm-2 control-label">Teléfono</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="ntelefono" autocomplete="off"/>
                  </div>
               </div>
               <div class="form-group">
                  <label for="nemail" class="col-lg-2 col-md-2 col-sm-2 control-label">Email</label>
                  <div class="col-lg-10 col-md-10 col-sm-10">
                     <input class="form-control" type="text" name="nemail" autocomplete="off"/>
                  </div>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span> &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>
{else}
<div class="text-center">
   <img src="view/img/fuuu_face.png" alt="fuuuuu"/>
</div>
{/if}

{include="footer"}